//
//  SEHospitalizeOrderListViewModel.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/28.
//

import UIKit

class SEHospitalizeOrderListViewModel: SEBaseViewModel {

    var models = [SEInquiryInfo]()

    var numberOfSections: Int {
        return models.count
    }

    var fromType: SEHospitalizeOrderListViewControllerFromType = .payHospitalize

    var selectedListViewModel = SEHospitalizeOrderDetailViewModel()

    func listViewModel(indexPath: IndexPath) -> SEHospitalizeOrderDetailViewModel {
        return SEHospitalizeOrderDetailViewModel(model: models[indexPath.section])
    }

    var cancelHandler: ((SEHttpStatus) -> Void)?

    /// 问诊订单列表
    func requestHospitalizeOrderList() {
        SEHttpManager.shared.request(SEHospitalizeAPI.listOfHospitalizeOrder(pageNum: page, pageSize: pageSize), model: SEHospitalizeOrderListModel.self) { (model, status) in
            switch status {
            case .success:
                if self.page == 1 {
                    self.models = model?.list ?? []
                } else {
                    self.models.append(contentsOf: model?.list ?? [])
                }
                self.hasMore = self.models.count < model?.total ?? 0
            default:
                self.page == 1 ? (self.page = 1) : (self.page -= 1)
            }
            self.updateData?(status)
        }
    }

    func cancelHospitalizeOrder() {
        SEHttpManager.shared.requestForAny(SEHospitalizeAPI.changeInquiryStatus(id: selectedListViewModel.model.id ?? "", status: "8"), withLoading: true) { result, status in
            self.cancelHandler?(status)
        }
    }

}
